/*
 * BordPaneel.java
 *
 * Created on December 5, 2008, 4:10 PM
 */

package tetris.view;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.Clip;
import tetris.model.blokken.Bord;

/**
 *
 * @author  Batist Leman & Pieter Hens
 */
public class BordPaneel extends javax.swing.JPanel implements Observer {
    private int aantalVakjesHorizontaal;
    private int aantalVakjesVerticaal;
    private Bord bord;
    Clip clip;
    
    /** Creates new form BordPaneel */
    public BordPaneel() {
        aantalVakjesHorizontaal = 10;
        aantalVakjesVerticaal = 18;
        bord = new Bord(10,18,1);
        initComponents();
        
        // File soundFile = new File("wreee.wav");
        //new AePlayWave("groovy.wav").start();


        
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.WHITE);
        //g.fillRect(0, 0, this.getWidth(),this.getHeight());
        g.setColor(Color.LIGHT_GRAY);
        for (int i = 1; i < aantalVakjesHorizontaal; i++) {
            g.drawLine(i * this.getWidth() / aantalVakjesHorizontaal,
                    1,
                    i * this.getWidth() / aantalVakjesHorizontaal,
                    this.getHeight() - 2);
            g.drawLine(i * this.getWidth() / aantalVakjesHorizontaal-1,
                    1,
                    i * this.getWidth() / aantalVakjesHorizontaal-1,
                    this.getHeight() - 2);
        }
        for (int i = 1; i < aantalVakjesVerticaal; i++) {
            g.drawLine(1,
                    i * getHeight() / aantalVakjesVerticaal,
                    this.getWidth() - 2,
                    i * getHeight() / aantalVakjesVerticaal);
            g.drawLine(1,
                    i * getHeight() / aantalVakjesVerticaal-1,
                    this.getWidth() - 2,
                    i * getHeight() / aantalVakjesVerticaal-1);
        }
        
        if (bord != null) {
            for (int i = 0; i < bord.getX(); i++) {
                for (int j = 0; j < bord.getY(); j++) {
                    if (bord.getBord()[i][j][0].isBezet()) {
                        g.setColor(bord.getBord()[i][j][0].getBlok().getKleur());
                        g.fillRect(i * this.getWidth() / aantalVakjesHorizontaal+1,
                                j * getHeight() / aantalVakjesVerticaal+1,
                                this.getWidth() / aantalVakjesHorizontaal-2,
                                getHeight() / aantalVakjesVerticaal-2);
                        g.setColor(Color.BLACK);
                        g.drawRect(i * this.getWidth() / aantalVakjesHorizontaal+1,
                                j * getHeight() / aantalVakjesVerticaal+1,
                                this.getWidth() / aantalVakjesHorizontaal-3,
                                getHeight() / aantalVakjesVerticaal-2);
                        /*g.drawRect(i * this.getWidth() / aantalVakjesHorizontaal,
                                j * getHeight() / aantalVakjesVerticaal,
                                this.getWidth() / aantalVakjesHorizontaal-1,
                                getHeight() / aantalVakjesVerticaal);*/
                    }
                }
            }
        }
        System.out.println("RePaintdone");
        

    }

    void setBord(Bord bord) {
        this.bord = bord;
    }
    
    
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 398, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void update(Observable arg0, Object arg1) {
        try {
            if (arg1 != null) {
              Thread.sleep(1); 
              new AePlayWave("groovy.wav").start();
            }
            repaint();
        } catch (InterruptedException ex) {
            Logger.getLogger(BordPaneel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
